/* Copyright (C) 2011  Till Schoenbein mail: tschoenbein[at]gmail.com
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
package de.shb.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.shb.DiceGenerator;
import de.shb.IDiceGenerator;

/**
 * Servlet implementation class ImageServlet
 */
public class RollDicesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RollDicesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		String topic = request.getParameter("topic");
		String post = request.getParameter("post");
		String dices = request.getParameter("dices");
		System.out.println(topic + " , " +post+ " , "+dices);
		
		IDiceGenerator dg = new DiceGenerator();
		
		File file = dg.execute(topic, post, dices);
		
		// Get the absolute path of the image
	    ServletContext sc = getServletContext();
	    //String filename = sc.getRealPath("ftank.jpg");
	    
	    System.out.println(file.canRead());
	    System.out.println(file.getAbsolutePath());
	    // Get the MIME type of the image
	    String mimeType = sc.getMimeType(file.getAbsolutePath());
	    if (mimeType == null) {
	        sc.log("Could not get MIME type of "+file.getName());
	        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	        return;
	    }

	    // Set content type
	    resp.setContentType(mimeType);

	    // Set content size
	    //File file = new File(filename);
	    resp.setContentLength((int)file.length());

	    // Open the file and output streams
	    FileInputStream in = new FileInputStream(file);
	    OutputStream out = resp.getOutputStream();

	    // Copy the contents of the file to the output stream
	    byte[] buf = new byte[1024];
	    int count = 0;
	    while ((count = in.read(buf)) >= 0) {
	        out.write(buf, 0, count);
	    }
	    in.close();
	    out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
